﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_DetailService : WebControlBase
{
    private long ServiceID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        ServiceID = Request.QueryString["serviceId"] != null ? Int32.Parse(Request.QueryString["serviceId"].ToString().Trim()) : -1;

        if (ClientState.MemberInfo.UserType != 0)
            this.lnkAdd.Enabled = false;
        else
            this.lnkAdd.Enabled = true;

        if (this.IsPostBack) return;

        if (ServiceID > 0)
        {
            this.BindObject();
        }
    }

    private void BindObject()
    {
        if (ServiceID != -1)
        {
            ServicesInfo objService = ServicesManager.Select(ServiceID);
            if (objService != null)
            {
                this.txtServiceBackup.Text = objService.ServiceBackup;
                this.txtServiceIndex.Text = objService.ServiceIndex.ToString();
                this.txtServiceName.Text = objService.ServiceName;
                this.ddlServiceID.Text = objService.ServiceNumber;

                if (objService.Status == 1)
                    this.chkStatus.Checked = true;
            }
        }
    }

    protected void lnkAdd_Click(object sender, EventArgs e)
    {
        try
        {
            if (ServiceID != -1)
            {
                ServicesInfo objService = ServicesManager.Select(ServiceID);
                if (objService != null)
                {
                    objService.ServiceName = this.txtServiceName.Text;
                    objService.ServiceBackup = this.txtServiceBackup.Text;
                    objService.ServiceIndex = byte.Parse(this.txtServiceIndex.Text.Trim());
                    objService.ServiceNumber = this.ddlServiceID.SelectedValue;
                    objService.Status = this.chkStatus.Checked ? (byte)1 : (byte)0;

                    bool result = ServicesManager.Update(objService);
                    if (result)
                        Response.Redirect("Default.aspx?module=ListService");
                    else
                        this.lblError.Text = "Cập nhật lỗi!";
                }
            }
            else
            {
                ServicesInfo objService = new ServicesInfo();
                objService.ServiceName = this.txtServiceName.Text;
                objService.ServiceBackup = this.txtServiceBackup.Text;
                objService.ServiceIndex = byte.Parse(this.txtServiceIndex.Text.Trim());
                objService.ServiceNumber = this.ddlServiceID.SelectedValue;
                objService.Status = this.chkStatus.Checked ? (byte)1 : (byte)0;

                bool result = ServicesManager.Insert(objService);
                if (result)
                    Response.Redirect("Default.aspx?module=ListService");
                else
                    this.lblError.Text = "Thêm mới lỗi!";
            }
        }
        catch { }
    }
}